<template>
  <form-item-container
    v-if="permission !== 'H'"
    :id="id"
    :item="item"
    :item-config="itemConfig"
    @select-item="selectItem"
    @delete-item="deleteItem"
    @copy-item="copyItem"
  >

    <a-range-picker
      v-model="formModel"
      popup-container="#RenderForm"
      :disabled="permission === 'D'"
      :readonly="permission === 'R'"
      :default-value="config.defaultValue"
      :placeholder="config.placeholder"
      :allow-clear="config.allowClear"
      :mode="config.mode"
      :format="config.format"
      :position="config.position"
      :show-time="config.showTime"
      :separator="config.separator"
      :abbreviation="config.abbreviation"
      :disabled-date="disableDate"
      :disabled-time="getDisabledRangeTime"
      @change="onChange"
    />
  </form-item-container>
</template>

<script lang="ts" setup>
import { inject, defineModel, defineProps, defineEmits, computed } from 'vue';
import { FormPermission } from '@/components/form-design/item-select/config/type';
import FormItemContainer from '@/components/form-design/form-container/container-item/form-item-container.vue';
import dayjs from 'dayjs';

const formModel = defineModel();

const props = defineProps({
  id: {
    type: String,
    default: () => ''
  },
  item: {
    type: Object,
    default: () => {
    }
  },
  itemConfig: {
    type: Object,
    default: () => {
    }
  },
  config: {
    type: Object,
    default: () => {
    }
  }
});

const formInstance = inject('formInstance');

const formPermission = inject<any>('formPermission');

const permission = computed<FormPermission>(() => {
  const result = formPermission[props.id];
  if (result) {
    return result;
  }
  return 'E';
});

const emits = defineEmits(['selectItem', 'deleteItem', 'copyItem']);

const selectItem = (value: any) => {
  emits('selectItem', props.item);
};

const deleteItem = (value: any) => {
  emits('deleteItem', props.item);
};

const copyItem = (value: any) => {
  emits('copyItem', props.item);
};

const disableDate = (current: any) => {
  if (props.config.disabledDate) {
    try {
      const disableDateCallBack = new Function('current', 'dayjs', props.config.disabledDate);
      return disableDateCallBack.call(formInstance, current, dayjs);
    } catch (e) {
      console.error(e);
    }
  }
  return false;
};

const range = (start: number, end: number) => {
  const result = [];
  for (let i = start; i < end; i += 1) {
    result.push(i);
  }
  return result;
};

const getDisabledRangeTime = (date: any, type: any) => {
  if (props.config.disabledTime) {
    try {
      const disabledTimeCallBack = new Function('date', 'type', 'range', props.config.disabledTime);
      return disabledTimeCallBack.call(formInstance, date, type, range);
    } catch (e) {
      console.error(e);
    }
  }
  return {
    disabledHours: () => [],
    disabledMinutes: () => [],
    disabledSeconds: () => []
  };
};

const onChange = (value: string) => {
  if (props.config.onChangeFunction) {
    try {
      const callBack = new Function('value', props.config.onChangeFunction);
      callBack.call(formInstance, value);
    } catch (e) {
      console.error(e);
    }
  }
};

</script>

<script lang="ts">
export default {
  name: 'FormItemDateRangePicker'
};
</script>

<style lang="less" scoped>

</style>